#! /bin/sh

source /etc/rc.d/common

ISCSID="/sbin/iscsid"
ISCSID_LOCK="/var/lock/iscsid"
ISCSIADM="/sbin/iscsiadm"
SYNC="/bin/sync"
INAME_FILE="/etc/iscsi/initiatorname.iscsi"
ISCSI_INAME="/sbin/iscsi-iname"
DFLT_OPTS=""

check_args ${@}

SCRIPT="$(/usr/bin/basename ${0})"
if check_opts_set ${SCRIPT}; then
    USER_OPTS="$(get_rc_opts ${SCRIPT})"
    if [ ${?} -ne 0 ]; then
        /bin/echo ${USER_OPTS}
        exit 1
    fi
else
    USER_OPTS="${DFLT_OPTS}"
fi

start() {
    if [ ! -f "${INAME_FILE}" ]; then
        /bin/echo "Generating unique iSCSI initiator name..."
	/bin/echo "InitiatorName=`${ISCSI_INAME}`" > ${INAME_FILE} || exit 1
    fi
    /bin/echo "Starting iSCSI initiator service..."
    eval ${ISCSID} ${USER_OPTS} || exit 1
    /bin/touch ${ISCSID_LOCK}
    /bin/echo "Setting up iSCSI targets..."
    /bin/sh -c "/usr/bin/nohup ${ISCSIADM} -m node \
        --loginall=automatic > /dev/null 2>&1 &"
}

stop() {
    /bin/echo "Stopping iSCSI initiator service..."
    ${SYNC}
    ${ISCSIADM} -m node --logoutall=all
    ${ISCSIADM} -k 0 && /bin/rm -f ${ISCSID_LOCK}
}

status() {
    /bin/pidof ${ISCSID} > /dev/null 2>&1
    exit ${?}
}

# Perform specified action
${1}
